package com.xnew.org.controller;

import com.xnew.aes.LogParameter;
import com.xnew.bean.common.OrgDeptInfo;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.org.service.OrgDeptService;
import com.xnew.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: hexs
 * @Project: JavaLaity
 * @Pcakage: com.xnew.org.controller.OrgDeptController
 * @Date: 2022年05月31日 16:50
 * @Description:
 */
@RestController
@RequestMapping(value = "/orgDept")
public class OrgDeptController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private OrgDeptService orgDeptService;

    /**
     *获取当前机构信息
     * @param input
     * @return
     */
    @LogParameter(key = "", desc = "获取当前机构信息")
    @RequestMapping(value = "/getCurrentLoginInfo", method = RequestMethod.POST)
    public DataOutput getCurrentLoginInfo(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            List<Map<String, String>> list = orgDeptService.getCurrentLoginInfo(userInfo);
            out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(list));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 组织结构查询列表
     * @param input
     * @return
     */
    @LogParameter(key = "", desc = "组织结构查询列表")
    @RequestMapping(value = "/queryOrgDeptList", method = RequestMethod.POST)
    public DataOutput queryOrgDeptList(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            List<OrgDeptInfo> list = orgDeptService.queryOrgDeptList(input, userInfo);
            out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(list));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 新增组织结构
     * @param input
     * @return
     */
    @LogParameter(key = "", desc = "新增组织结构")
    @RequestMapping(value = "/addOrgDept", method = RequestMethod.POST)
    public DataOutput addOrgDept(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        OrgDeptInfo orgDeptInfo = new OrgDeptInfo();
        if (
            input.getParam() == null ||
            input.getParam().get("deptName") == null ||
            input.getParam().get("deptBeltoRegiCode") == null ||
            StringUtils.isEmpty(input.getParam().get("deptName").toString()) ||
            StringUtils.isEmpty(input.getParam().get("deptBeltoRegiCode").toString())
        ){
            out.setCode(-90001);
            out.setMsg("必填字段不存在！");
            return out;
        }
        try {
            orgDeptInfo =(OrgDeptInfo) ConvertEntityUtil.convertEntityByParam(input.getParam(), orgDeptInfo);
            out = orgDeptService.addOrgDept(orgDeptInfo, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 修改组织结构
     * @param input
     * @return
     */
    @LogParameter(key = "", desc = "修改组织结构")
    @RequestMapping(value = "/updateOrgDept", method = RequestMethod.POST)
    public DataOutput updateOrgDept(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        OrgDeptInfo orgDeptInfo = new OrgDeptInfo();
        if (
            input.getParam() == null ||
            input.getParam().get("deptNo") == null ||
            input.getParam().get("deptName") == null ||
            input.getParam().get("deptBeltoRegiCode") == null ||
            StringUtils.isEmpty(input.getParam().get("deptNo").toString()) ||
            StringUtils.isEmpty(input.getParam().get("deptName").toString()) ||
            StringUtils.isEmpty(input.getParam().get("deptBeltoRegiCode").toString())
        ){
            out.setCode(-90001);
            out.setMsg("必填字段不存在！");
            return out;
        }
        try {
            orgDeptInfo =(OrgDeptInfo) ConvertEntityUtil.convertEntityByParam(input.getParam(), orgDeptInfo);
            out = orgDeptService.updateOrgDept(orgDeptInfo, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 查询组织结构
     * @param input
     * @return
     */
    @LogParameter(key = "", desc = "查询组织结构")
    @RequestMapping(value = "/queryOrgDeptById", method = RequestMethod.POST)
    public DataOutput queryOrgDeptById(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        if (
            input.getParam() == null ||
            input.getParam().get("deptNo") == null ||
            StringUtils.isEmpty(input.getParam().get("deptNo").toString())
        ){
            out.setCode(-90001);
            out.setMsg("必填字段不存在！");
            return out;
        }
        try {
            out.setData(ConvertEntityUtil.convertEntityData(orgDeptService.queryOrgDeptById(input.getParam().get("deptNo").toString())));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }

    /**
     * 删除组织结构
     * @param input
     * @return
     */
    @LogParameter(key = "", desc = "删除组织结构")
    @RequestMapping(value = "/deleteOrgDeptById", method = RequestMethod.POST)
    public DataOutput deleteOrgDeptById(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        if (
            input.getParam() == null ||
            input.getParam().get("deptNo") == null ||
            StringUtils.isEmpty(input.getParam().get("deptNo").toString())
        ){
            out.setCode(-90001);
            out.setMsg("必填字段不存在！");
            return out;
        }
        try {
            orgDeptService.deleteOrgDeptById(input.getParam().get("deptNo").toString());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            out.setCode(-90001);
            out.setMsg("操作失败！");
        }
        return out;
    }
}
